﻿
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web.WebPages.Html;

namespace App_Code
{
    /// <summary>
    /// Summary description for BustimesController
    /// </summary>
    public class BustimesController
    {
        readonly MySQLHelper _sql = new MySQLHelper();

        public BustimesController()
        {
            _sql.ReadRoutesDataSet();
        }

        public IEnumerable<SelectListItem> GetBusStops()
        {
            if (!_sql._ds.Tables.Contains(MySQLHelper.BUS_QUERY))
                return new List<SelectListItem>();

            return (from DataRow row in _sql._ds.Tables[MySQLHelper.BUS_QUERY].Rows
                    let id = "" + row[0]
                    let busStopName = "" + row[2] + ", " + row[1]
                    select new SelectListItem {Value = id, Text = busStopName}).ToList();
        }

        // you will be punished severly if you change the return type to Dictionary<string, string>
        // Yes typing is good - except when it isn't
        public Dictionary<string, object> GetAttributes()
        {
            var dict = new Dictionary<string, object>
                           {
                               {"onChange", "reportMatrix();"},
                               {"onkeyup", "reportMatrix();"},
                               {"style", "height:26px;width:320px;margin-left: 0px"}
                           };
            return dict;
        }


        public PlaceCoordinate GetPlaceCoordinate(string id)
        {
            try
            {
                foreach (DataRow row in _sql._ds.Tables[MySQLHelper.BUS_QUERY].Rows)
                {
                    if ("" + (int)row[0] == id)
                    {
                        return new PlaceCoordinate((float)row[4], (float)row[5], (int)row[6]);
                    }
                }
            }
            catch (Exception)
            {
                return new PlaceCoordinate(20, 100, 8);
            }
            return new PlaceCoordinate(20, 100, 8);
        }
    }
}